package com.jarchie.htgl.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;

import com.jarchie.htgl.R;
import com.jarchie.htgl.base.BaseActivity;
import com.jarchie.htgl.fragment.ArchiveQueFragment;
import com.jarchie.htgl.fragment.ContractManFragment;
import com.jarchie.htgl.fragment.FoundationDicFragment;
import com.jarchie.htgl.fragment.PwdManageFragment;
import com.jarchie.htgl.utils.CommonUtils;
import com.luseen.luseenbottomnavigation.BottomNavigation.BottomNavigationItem;
import com.luseen.luseenbottomnavigation.BottomNavigation.BottomNavigationView;
import com.luseen.luseenbottomnavigation.BottomNavigation.OnBottomNavigationItemClickListener;

/**
 * Created by Jarchie on 2017\12\11.
 * 项目的主Activity，控制切换显示四个Fragment
 */
public class MainActivity extends BaseActivity implements OnBottomNavigationItemClickListener {
    private Fragment contractManFgt, archiveQueFgt, foundationDicFgt, pwdManageFgt;
    private BottomNavigationView mNavigationView;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        mNavigationView = obtainView(R.id.navigation_view);
    }

    @Override
    protected void setListener() {
        mNavigationView.setOnBottomNavigationItemClickListener(this);
    }

    @Override
    protected void initData() {
        initNavigationView();
        mNavigationView.selectTab(0);
        setSelect(0);
    }

    @Override
    public void onNavigationItemClick(int index) {
        switch (index) {
            case 0:
                setSelect(0);
                break;
            case 1:
                setSelect(1);
                break;
            case 2:
                setSelect(2);
                break;
            case 3:
                setSelect(3);
                break;
        }
    }

    //初始化BottomNavagationView
    private void initNavigationView() {
        if (mNavigationView != null) {
            mNavigationView.isWithText(true); //设置汉字一直显示
            //整体背景色，false是icon和文字显示颜色能用，此时是默认的颜色
            mNavigationView.isColoredBackground(false);
            mNavigationView.setItemActiveColorWithoutColoredBackground(ContextCompat.getColor(this, R.color.color_text_dark));
            mNavigationView.disableShadow(); //禁用底部阴影
        }
        BottomNavigationItem htglItem = new BottomNavigationItem(getString(R.string.tab_htgl), ContextCompat.getColor(this, R.color.color_text_dark), R.drawable.tab_htgl);
        BottomNavigationItem gdcxItem = new BottomNavigationItem(getString(R.string.tab_gdcx), ContextCompat.getColor(this, R.color.color_text_dark), R.drawable.tab_gdcx);
        BottomNavigationItem jczdItem = new BottomNavigationItem(getString(R.string.tab_jczd), ContextCompat.getColor(this, R.color.color_text_dark), R.drawable.tab_jczd);
        BottomNavigationItem klxgItem = new BottomNavigationItem(getString(R.string.tab_klxg), ContextCompat.getColor(this, R.color.color_text_dark), R.drawable.tab_klxg);
        mNavigationView.addTab(htglItem);
        mNavigationView.addTab(gdcxItem);
        mNavigationView.addTab(jczdItem);
        mNavigationView.addTab(klxgItem);
    }

    //控制切换显示四个Fragment
    private void setSelect(int pos) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        hideFragment(transaction);
        switch (pos) {
            case 0: //合同管理
                if (contractManFgt == null) {
                    contractManFgt = new ContractManFragment();
                    transaction.add(R.id.id_content, contractManFgt);
                } else {
                    transaction.show(contractManFgt);
                }
                break;
            case 1: //归档查询
                if (archiveQueFgt == null) {
                    archiveQueFgt = new ArchiveQueFragment();
                    transaction.add(R.id.id_content, archiveQueFgt);
                } else {
                    transaction.show(archiveQueFgt);
                }
                break;
            case 2: //基础字典
                if (foundationDicFgt == null) {
                    foundationDicFgt = new FoundationDicFragment();
                    transaction.add(R.id.id_content, foundationDicFgt);
                } else {
                    transaction.show(foundationDicFgt);
                }
                break;
            case 3: //口令修改
                if (pwdManageFgt == null) {
                    pwdManageFgt = new PwdManageFragment();
                    transaction.add(R.id.id_content, pwdManageFgt);
                } else {
                    transaction.show(pwdManageFgt);
                }
                break;
        }
        transaction.commit();
    }

    //隐藏Fragment
    private void hideFragment(FragmentTransaction transaction) {
        if (contractManFgt != null) {
            transaction.hide(contractManFgt);
        }
        if (archiveQueFgt != null) {
            transaction.hide(archiveQueFgt);
        }
        if (foundationDicFgt != null) {
            transaction.hide(foundationDicFgt);
        }
        if (pwdManageFgt != null) {
            transaction.hide(pwdManageFgt);
        }
    }

    @Override
    public void onBackPressed() { //双击退出程序
        CommonUtils.exitApp(this);
    }

}